package com.my.chapter17;

import net.mindview.util.Countries;

import java.util.Iterator;
import java.util.LinkedList;

public class practice22 {
    public static void main(String[] args) {
        SimpleHashMap4<String,String> m =
                new SimpleHashMap4<String,String>();
        m.putAll(Countries.capitals(10));
        System.out.println(m);
        System.out.println("m.get(\"BURUNDI\") = "
                + m.get("BURUNDI"));
        m.remove("BURUNDI");
        System.out.println(
                "After removal, m.get(\"BURUNDI\") = "
                        + m.get("BURUNDI"));
        m.clear();
        System.out.println("After clearing: " + m);
    }
}

class SimpleHashMap4<K,V> extends SimpleHashMap<K,V> {
    @SuppressWarnings("unchecked")
    public void clear() {
        // Effectively erase everything by allocating
        // a new empty array of buckets:
        buckets = new LinkedList[SIZE];
    }
    public V remove(Object key) {
        int index = Math.abs(key.hashCode()) % SIZE;
        if(buckets[index] == null) return null;
        Iterator<MapEntry<K,V>> it = buckets[index].iterator();
        while(it.hasNext()) {
            MapEntry<K,V> iPair = it.next();
            if(iPair.getKey().equals(key)) {
                V value = iPair.getValue();
                it.remove();
                return value;
            }
        }
        return null;
    }
}
